using System;
using System.Web.Security;
using System.Security.Principal;
using System.Web.Script.Serialization;

namespace Web.Core.Security
{
    public static class IdentityExtension
    {
        public static Identity Identity(this IIdentity identity)
        {
            var id = identity as FormsIdentity;
            if (id != null)
            {
                var data = id.Ticket.UserData;
                try
                {
                    var serializer = new JavaScriptSerializer();
                    return serializer.Deserialize<Identity>(data);
                }
                catch (Exception)
                {
                    return new Identity();
                }
            }
            return new Identity();
        }
    }
}